<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生表单提交系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        h1, h2 {
            color: #333;
            text-align: center;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"], input[type="tel"], input[type="number"], input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            box-sizing: border-box;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
            margin-top: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .error {
            color: #dc3545;
            margin-top: 10px;
        }
        .success {
            color: #28a745;
            margin-top: 10px;
        }
        .hidden {
            display: none;
        }
        .admin-section {
            border-top: 2px solid #007bff;
            margin-top: 30px;
            padding-top: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        .pagination {
            text-align: center;
            margin-top: 20px;
        }
        .pagination button {
            width: auto;
            margin: 0 5px;
            padding: 8px 16px;
        }
    </style>
</head>
<body>
    <!-- 学生表单提交区域 -->
    <div class="container">
        <h1>学生信息提交</h1>
        <form id="studentForm">
            <div class="form-group">
                <label for="name">姓名：</label>
                <input type="text" id="name" name="name" required>
            </div>
            <div class="form-group">
                <label for="phone">手机号码：</label>
                <input type="tel" id="phone" name="phone" required>
            </div>
            <div class="form-group">
                <label for="grade">年级：</label>
                <input type="number" id="grade" name="grade" min="1" max="12" required>
            </div>
            <div class="form-group">
                <label for="age">年龄：</label>
                <input type="number" id="age" name="age" min="5" max="25" required>
            </div>
            <button type="submit">提交表单</button>
        </form>
        <div id="studentMessage"></div>
    </div>

    <!-- 管理员登录区域 -->
    <div class="container admin-section">
        <h2>管理员登录</h2>
        <div id="loginSection">
            <form id="loginForm">
                <div class="form-group">
                    <label for="username">用户名：</label>
                    <input type="text" id="username" name="username" required>
                </div>
                <div class="form-group">
                    <label for="password">密码：</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <button type="submit">登录</button>
            </form>
            <div id="loginMessage"></div>
        </div>

        <!-- 管理员面板 -->
        <div id="adminPanel" class="hidden">
            <h3>提交记录管理</h3>
            <button id="loadForms">刷新数据</button>
            <button id="logoutBtn">登出</button>
            <div id="formsContainer"></div>
            <div id="pagination"></div>
        </div>
    </div>

    <script>
        const API_BASE = 'http://localhost:5000/api';
        let currentPage = 1;

        // 学生表单提交
        document.getElementById('studentForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const formData = {
                name: document.getElementById('name').value.trim(),
                phone: document.getElementById('phone').value.trim(),
                grade: parseInt(document.getElementById('grade').value),
                age: parseInt(document.getElementById('age').value)
            };

            try {
                const response = await fetch(`${API_BASE}/submit-form`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    credentials: 'include',
                    body: JSON.stringify(formData)
                });

                const result = await response.json();
                const messageDiv = document.getElementById('studentMessage');

                if (result.success) {
                    messageDiv.innerHTML = `<div class="success">${result.message}</div>`;
                    document.getElementById('studentForm').reset();
                } else {
                    let errorMsg = result.message;
                    if (result.errors) {
                        errorMsg += '<br>' + result.errors.join('<br>');
                    }
                    messageDiv.innerHTML = `<div class="error">${errorMsg}</div>`;
                }
            } catch (error) {
                document.getElementById('studentMessage').innerHTML = 
                    `<div class="error">网络错误，请检查服务器是否启动</div>`;
            }
        });

        // 管理员登录
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const loginData = {
                username: document.getElementById('username').value.trim(),
                password: document.getElementById('password').value
            };

            try {
                const response = await fetch(`${API_BASE}/admin/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    credentials: 'include',
                    body: JSON.stringify(loginData)
                });

                const result = await response.json();
                const messageDiv = document.getElementById('loginMessage');

                if (result.success) {
                    messageDiv.innerHTML = `<div class="success">${result.message}</div>`;
                    document.getElementById('loginSection').classList.add('hidden');
                    document.getElementById('adminPanel').classList.remove('hidden');
                    loadForms();
                } else {
                    messageDiv.innerHTML = `<div class="error">${result.message}</div>`;
                }
            } catch (error) {
                document.getElementById('loginMessage').innerHTML = 
                    `<div class="error">网络错误，请检查服务器是否启动</div>`;
            }
        });

        // 加载表单数据
        async function loadForms(page = 1) {
            try {
                const response = await fetch(`${API_BASE}/admin/forms?page=${page}&per_page=10`, {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    credentials: 'include'
                });

                const result = await response.json();

                if (result.success) {
                    displayForms(result.data.forms);
                    displayPagination(result.data.pagination);
                } else {
                    document.getElementById('formsContainer').innerHTML = 
                        `<div class="error">${result.message}</div>`;
                }
            } catch (error) {
                document.getElementById('formsContainer').innerHTML = 
                    `<div class="error">加载数据失败</div>`;
            }
        }

        // 显示表单数据
        function displayForms(forms) {
            const container = document.getElementById('formsContainer');
            
            if (forms.length === 0) {
                container.innerHTML = '<p>暂无提交记录</p>';
                return;
            }

            let html = `
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>姓名</th>
                            <th>电话</th>
                            <th>年级</th>
                            <th>年龄</th>
                            <th>提交时间</th>
                        </tr>
                    </thead>
                    <tbody>
            `;

            forms.forEach(form => {
                html += `
                    <tr>
                        <td>${form.id}</td>
                        <td>${form.name}</td>
                        <td>${form.phone}</td>
                        <td>${form.grade}年级</td>
                        <td>${form.age}岁</td>
                        <td>${form.created_at}</td>
                    </tr>
                `;
            });

            html += '</tbody></table>';
            container.innerHTML = html;
        }

        // 显示分页
        function displayPagination(pagination) {
            const container = document.getElementById('pagination');
            let html = '';

            if (pagination.has_prev) {
                html += `<button onclick="loadForms(${pagination.page - 1})">上一页</button>`;
            }

            html += `<span>第 ${pagination.page} 页，共 ${pagination.pages} 页</span>`;

            if (pagination.has_next) {
                html += `<button onclick="loadForms(${pagination.page + 1})">下一页</button>`;
            }

            container.innerHTML = html;
        }

        // 刷新数据
        document.getElementById('loadForms').addEventListener('click', () => {
            loadForms(currentPage);
        });

        // 登出
        document.getElementById('logoutBtn').addEventListener('click', async () => {
            try {
                await fetch(`${API_BASE}/admin/logout`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    credentials: 'include'
                });

                document.getElementById('loginSection').classList.remove('hidden');
                document.getElementById('adminPanel').classList.add('hidden');
                document.getElementById('loginForm').reset();
                document.getElementById('loginMessage').innerHTML = '';
            } catch (error) {
                console.error('登出失败:', error);
            }
        });
    </script>
</body>
</html>